package cn.zyl.demo.undertow.controller;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.PushBuilder;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

/**
 * UndertowIndexController类说明:
 *
 * @author z.y.l
 * @version v1.0
 * @date 2020-09-01
 */
@Controller
@RequestMapping("/")
public class UndertowIndexController {
    @RequestMapping("")
    @ResponseBody
    public String root(){
        return "Hello demo undertow server.";
    }
    @RequestMapping("index")
    public String index(){
        System.out.println("-----");
        return "index";
    }
    @RequestMapping("hello")
    @ResponseBody
    public String hello(){
        return "Hello Undertow.";
    }
    //测试http2 的server push
//    @GetMapping("/push")
//    @ResponseBody
//    public void http2ServerPush(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        PushBuilder pushBuilder = request.newPushBuilder();
//        pushBuilder.path("/demo.png").addHeader("content-type", "image/png").push();
//        try(PrintWriter respWriter = response.getWriter()){
//            respWriter.write("<html><img src='/demo.png'></html>");
//        }
//    }
}
